//
//  MainTabBarController.m
//  MiAiApp
//
//  Created by 吴欧 on 2020/09/28.
//  Copyright © 2020年 honglutec. All rights reserved.
//

#import "MainTabBarController.h"

#import "RootNavigationController.h"
#import "HomeViewController.h"
 
#import "MessageViewController.h"
#import "PreferViewController.h"
#import "RankViewController.h"
#import "MineViewController.h"
#import "HEOrgMapViewController.h"
#import "HEPatientsCircleViewController.h"
#import "HECireleListViewController.h"

#import "UITabBar+CustomBadge.h"
#import "XYTabBar.h"
#import "HEAITabBar.h"
#import "HEAIAudioView.h"
#import "HESearchData.h"

#import "HEUpdateAlert.h"
#import "HEHomeViewController.h"
#import "HEHealthEduViewController.h"

@interface MainTabBarController ()<UITabBarControllerDelegate, GDCenterTabBarDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC

@property (nonatomic, strong) HESearchData *searchData;

@end

@implementation MainTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化tabbar
    [self setUpTabBar];
    
    //添加子控制器
    [self setUpAllChildViewController];

    // 检测版本更新
    [HEUpdateAlert checkNewEditionWithAppID:APPID ctrl:self];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:KChangeFontNotification object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KChangeFontNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)refresh {
    for (UITabBarItem *view in self.tabBar.items) {
            //未选中字体颜色
        [view setTitleTextAttributes:@{NSForegroundColorAttributeName:KBlackColor,NSFontAttributeName:DYFont(12.0f)} forState:UIControlStateNormal];
        //选中字体颜色
        [view setTitleTextAttributes:@{NSForegroundColorAttributeName:CNavBgColor,NSFontAttributeName:DYFont(12.0f)} forState:UIControlStateSelected];
    }
    [self.tabBar layoutSubviews];
}


#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    
    HEAITabBar *tabBar = [[HEAITabBar alloc] init];
    tabBar.delegate = self;
    [self setValue:tabBar forKey:@"tabBar"];
    
    //设置背景色 去掉分割线
    [self setValue:tabBar forKey:@"tabBar"];
 
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}

#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    _VCS = @[].mutableCopy;
 
    [self setupChildViewController:[[HEHomeViewController alloc]init] title:@"首页" imageName:@"home" seleceImageName:@"home_select"];
     [self setupChildViewController:[[MessageViewController alloc]init] title:@"消息" imageName:@"message" seleceImageName:@"message_select"];
//    [self setupChildViewController:[[PreferViewController alloc]init] title:@"优惠" imageName:@"prefer" seleceImageName:@"prefer_select"];
//    [self setupChildViewController:[[HEOrgMapViewController alloc]init] title:@"机构地图" imageName:@"rank" seleceImageName:@"rank_select"];
//    [self setupChildViewController:[[HEPatientsCircleViewController alloc]init] title:@"圈子" imageName:@"group" seleceImageName:@"group_select"];
    
//    [self setupChildViewController:[[HECireleListViewController alloc]init] title:@"圈子" imageName:@"group" seleceImageName:@"group_select"];

    HEHealthEduViewController *eduVC = [[HEHealthEduViewController alloc] init];
    eduVC.needGestureCompatible = NO;
    [self setupChildViewController: eduVC title:@"宣教" imageName:@"group" seleceImageName:@"group_select"];

    [self setupChildViewController:[[MineViewController alloc]init] title:@"我的" imageName:@"mine" seleceImageName:@"mine_select"];
 
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    //未选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KBlackColor,NSFontAttributeName:DYFont(12.0f)} forState:UIControlStateNormal];
    
    //选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CNavBgColor,NSFontAttributeName:DYFont(12.0f)} forState:UIControlStateSelected];
    //包装导航控制器
    RootNavigationController *nav = [[RootNavigationController alloc] initWithRootViewController:controller];
    
//    [self addChildViewController:nav];
    [_VCS addObject:nav];
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //    NSLog(@"选中 %ld",tabBarController.selectedIndex);
    
}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:0 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

#pragma mark - GDCenterTabBarDelegate
- (void)tabBarCenterButtonClick:(HEAITabBar *)tabBar {
    
    HEAIAudioView *aiAudio = [HEAIAudioView new];
    [aiAudio show];
    [aiAudio finishAudioSearchHandler:^(NSString * _Nonnull searchStr) {
        if ([DYTools isBlankString:searchStr]) {
            [Dialog toastBottom:@"未识别到关键词"];
            return;
        }
        
        self.searchData = [HESearchData new];
        
        RootNavigationController *nav = (RootNavigationController *)self.selectedViewController;
        self.searchData.superVC = nav.viewControllers.firstObject;
        self.searchData.searchStr = searchStr;
        [self.searchData toSearchVC];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
